package com.preguicoso.client.estabelecimento.cardapio;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.preguicoso.client.estabelecimento.carrinho.Carrinho;
import com.preguicoso.client.telaInicial.lightBox;
import com.preguicoso.shared.entities.CategoriaBean;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.ChangeEvent;

public class Cardapio extends Composite {
	private static CardapioUiBinder uiBinder = GWT
			.create(CardapioUiBinder.class);

	@UiField
	HTMLPanel cardapio;
	Label categoria;
	@UiField
	HTMLPanel listaItensCategoria;
	@UiField
	Image loading;
	@UiField
	HTMLPanel checkout;
	@UiField HTMLPanel busca;
	@UiField TextBox buscaBox;

	Carrinho carrinho = Carrinho.getInstance();
	
	private final CardapioServiceAsync cardapioService = GWT
			.create(CardapioService.class);

	protected Button botaoPedir = new Button("Pagar");

	interface CardapioUiBinder extends UiBinder<Widget, Cardapio> {
	}

	private static Cardapio instance = null;
	public static Cardapio getInstance() {
		if (instance == null) {
		
				instance = new Cardapio();
		}
		return instance;
	}
	
	private Cardapio() {
		this.initWidget(uiBinder.createAndBindUi(this));
		busca.setVisible(false);
		cardapio.setHeight((Window.getClientHeight()-50)+"px");
	}

	
	ArrayList<CardapioCategoria> listaCategoria = new ArrayList<CardapioCategoria>();
	
	public Cardapio(final long id) {
		this.initWidget(uiBinder.createAndBindUi(this));
		this.carrinho.refreshCarrinho();
		

		lightBox.getInstance().showLoading("Carregando cardápio...");		
		this.cardapioService.getCategorias(id, new AsyncCallback<ArrayList<CategoriaBean>>() {
			
			@Override
			public void onSuccess(ArrayList<CategoriaBean> result) {
				
				
				for (CategoriaBean categoriaBean : result) {
					CardapioCategoria c = new CardapioCategoria(categoriaBean);
					
					listaCategoria.add(c);
					listaItensCategoria.add(c);
				}
				lightBox.getInstance().hide();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	public void loadingImage(String nome) {
		Image carregando = new Image("img/ajax-loader.gif");
		RootPanel.get("pedido").add(carregando);
	}

	public void atualizarLista(String text) {
		// TODO Auto-generated method stub
		
	}

	@UiHandler("buscaBox")
	void onBuscaBoxChange(ChangeEvent event) {
		lightBox.getInstance().show(new HTMLPanel("Buscando..."), 100, 100);
		for (CardapioCategoria t : listaCategoria) {
			t.atualizarCategoria(buscaBox.getText());			
		}
		lightBox.getInstance().hide();
	}
	@UiHandler("buscaBox")
	void onBuscaBoxClick(ClickEvent event) {
		buscaBox.setText("");
	}
}
